<?php
	
    function checkLogin ( $levels )
	{
		if(!$_SESSION['logged_in'])
		{
			$access = FALSE;
		}
		else {
			$kt = split(' ', $levels);
			
			$query = mysql_query('SELECT status FROM user WHERE id = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
			$row = mysql_fetch_assoc($query);
			
			$access = FALSE;
			
			while(list($key,$val)=each($kt))
			{
				if($val==$row['status'])
				{//Okie! Chap nhan quyen log in
					$access = TRUE;
				}
			}
		}
		if($access==FALSE)
		{
			header("Location: login.php");
		}
		
	}
	

	function valid_email($str)
	{
		return ( ! preg_match ( "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str ) ) ? FALSE : TRUE;
	}

	 
	function checkUnique($field, $compared)
	{
		$query = mysql_query ( "SELECT `" . mysql_real_escape_string ( $field ) . "` FROM `user` WHERE `" . mysql_real_escape_string ( $field ) . "` = '" . mysql_real_escape_string ( $compared ) . "'" );
		if ( mysql_num_rows ( $query ) == 0 )
		{
			return TRUE;
		}
		else {
			return FALSE;
		}
	}

	// Lay username tu id cua nguoi vua log in
	 
	function get_username ( $id )
	{
		$query = mysql_query("SELECT name FROM user WHERE id= '" . mysql_real_escape_string ( $id ) . "'");
		
		if ( mysql_num_rows ( $query ) == 1 )
		{
			$row = @mysql_fetch_array ( $query ); 
			
			return $row['name'];
		}
		else {
			return FALSE;
		}
	}

?>